/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: system_init.cpp
 *
 * Purpose: system initialize process, first process to run.
 *
 * Developer:
 *   wen.gu , 2019-09-29
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/

#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <string>
#include <chrono>
#include <thread>

/******************************************************************************
 **    MACROS
 ******************************************************************************/
#define LOGD printf
#define LOGI printf
#define LOGE printf

#ifndef F_OK
#define F_OK 0
#endif

#if defined(_WIN32)
#define PATH_SLASH '\\'
#else
#define PATH_SLASH '/'
#endif

#if defined(_WIN32)
#define access(a, b) _access(a, b)
#endif

/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/



/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/

void launchInitServices(const std::string& pandaRootPath)
{
    /**====================================================================*/
     /** launch property service  */
    std::string  propSrvCmd = pandaRootPath;

#if defined(_WIN32)
    propSrvCmd += "start /b" + propSrvCmd + "bin\\PropertyService.exe " + pandaRootPath;
#else
    propSrvCmd += "bin/PropertyService " + pandaRootPath + " &";
#endif   

    system(propSrvCmd.c_str());
    
    /** sleep a while wait property service boot complete, todo refine me?? */
    std::this_thread::sleep_for(std::chrono::milliseconds(500)); 
    /**====================================================================*/
    /** launch boot manager service */
    std::string  bootManagerCmd = pandaRootPath;

#if defined(_WIN32)
    bootManagerCmd += "start /b" + bootManagerCmd + "bin\\BootManager.exe " + pandaRootPath;
#else
    bootManagerCmd += "bin/BootManager " + pandaRootPath + " &";
#endif   

    system(bootManagerCmd.c_str());

    /**====================================================================*/
    //todo refine me??
}


int main(int argc, char* argv[])
{
    LOGI("system initialize start...\n");
    if (argc >= 2)
    {
        char* pandaRootPath = argv[1];

        if (pandaRootPath)
        {
            if (access(pandaRootPath, F_OK) == 0)
            {
                std::string rootDir = pandaRootPath;
                if (rootDir.back() != PATH_SLASH)
                {
                    rootDir.append(1, PATH_SLASH);
                }

                launchInitServices(rootDir);
                LOGI("system initialize successed...\n");
            }
            else
            {
                LOGE("the root path(%s) of panda os not exist\n", pandaRootPath);
            }
        }
        else
        {
            LOGE("the root path of panda os is nullptr\n");
        }
    }
    else
    {
        LOGE(" need set the root path of panda os\n");
    }

    return 0;
}
